package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.blog.dao.BlogStatusDao;
import com.wei.czz.framework.blog.entity.BlogStatusEntity;
import com.wei.czz.framework.blog.service.BlogStatusService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-11-29 20:25:08
 * className: BlogStatusServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogStatusService")
public class BlogStatusServiceImpl extends ServiceImpl<BlogStatusDao, BlogStatusEntity> implements BlogStatusService {

    private static final Logger log = LoggerFactory.getLogger(BlogStatusServiceImpl.class);

    @Override
    public void saveOrEdit(BlogStatusEntity blogStatus) {
        // 获取操作用户
        String username = SecurityUtils.getOrDefaultUsername(StringUtils.EMPTY);
        Long userId = SecurityUtils.getOrDefaultUserId(CommonEnum.ZERO.getLongValue());
        Date date = new Date();

        if (Objects.isNull(blogStatus.getId())) {
            blogStatus.setDeleteStatus(CommonEnum.ZERO.getValue())
                    .setUpdateTime(date)
                    .setUpdateUser(StringUtils.EMPTY)
                    .setUpdateUserId(CommonEnum.ZERO.getLongValue())
                    .setCreateTime(date)
                    .setCreateUser(username)
                    .setCreateUserId(userId);

            int count = baseMapper.insert(blogStatus);
            log.info("保存博客状态记录成功。count={}", count);
        } else {
            blogStatus.setUpdateTime(date)
                    .setUpdateUser(username)
                    .setUpdateUserId(userId);

            int count = baseMapper.updateById(blogStatus);
            log.info("修改博客状态记录成功。count={}", count);
        }
    }

    @Override
    public BlogStatusEntity findLast(Long blogId) {

        LambdaQueryWrapper<BlogStatusEntity> blogStatusLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogStatusLambdaQueryWrapper.eq(BlogStatusEntity::getBlogId, blogId)
                .eq(BlogStatusEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 排序
        blogStatusLambdaQueryWrapper.orderByDesc(BlogStatusEntity::getId);
        // 查询一条数据
        blogStatusLambdaQueryWrapper.last("limit 1");
        // 查询
        List<BlogStatusEntity> blogStatusList = baseMapper.selectList(blogStatusLambdaQueryWrapper);
        log.info("查询博客状态记录完成。list.size={}", blogStatusList.size());
        if (CollectionUtils.isEmpty(blogStatusList)) {
            throw new CzzException(ResultEnum.NOT_FOUND.getCode(), "博客状态记录不存在，请确认");
        }
        return blogStatusList.get(0);
    }

    @Override
    public BlogStatusEntity findLastStatus(Long blogId, Integer preStatus, Integer curStatus) {
        if (Objects.isNull(blogId)) {
            log.info("获取最新一条指定博客状态记录，传递的博客主键为空");
            throw new CzzException();
        }
        if (ObjectUtils.allNull(preStatus, curStatus)) {
            log.info("获取最新一条指定博客状态记录，传递的变更前状态和最新状态均为空");
            throw new CzzException();
        }
        LambdaQueryWrapper<BlogStatusEntity> blogStatusLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogStatusLambdaQueryWrapper.eq(BlogStatusEntity::getBlogId, blogId)
                .eq(Objects.nonNull(preStatus), BlogStatusEntity::getPreStatus, preStatus)
                .eq(Objects.isNull(curStatus), BlogStatusEntity::getCurStatus, curStatus)
                .eq(BlogStatusEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询一条数据
        blogStatusLambdaQueryWrapper.last("limit 1");
        // 查询
        List<BlogStatusEntity> blogStatusList = baseMapper.selectList(blogStatusLambdaQueryWrapper);
        log.info("查询最新一条指定博客状态记录完成。list.size={}", blogStatusList.size());
        if (CollectionUtils.isEmpty(blogStatusList)) {
            throw new CzzException(ResultEnum.NOT_FOUND.getCode(), "博客状态记录不存在，请确认");
        }
        return blogStatusList.get(0);
    }
}